
package com.ydd.common.controller;


import com.alibaba.excel.EasyExcel;
import com.ydd.common.dto.excel.ExampleExport;
import com.ydd.common.exception.BusinessException;
import com.ydd.common.listener.TListener;
import com.ydd.common.listener.model.ExampleModel;
import com.ydd.common.result.ResponseResult;
import com.ydd.common.utils.excel.poi.easypoi.EasyPoiUtil;
import com.ydd.common.utils.excel.poi.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 使用demo
 */
@RestController
@RequestMapping("/demo")
@Api(tags = "demoController")
public class DemoController {
    @RequestMapping(value = "/demoEasyExcelPoi", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "demo导出excel")
    public void demoEasyExcelPoi(HttpServletResponse response) {
        ArrayList<ExampleExport> objects = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            ExampleExport excelInfoExport = new ExampleExport();
            excelInfoExport.setCel(i + "---");
            objects.add(excelInfoExport);
        }
        Workbook workbook = EasyPoiUtil.exportExcel(ExampleExport.class, objects);
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        try {
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + URLEncoder.encode("导入查询.xls", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            throw new BusinessException("导出失败");
        }
    }

    @RequestMapping(value = "/demoExcelPoi", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "demo导出excel")
    public void demoExcelPoi(HttpServletResponse response) {
        ArrayList<String> rows = new ArrayList<>();
        rows.add("列1");
        rows.add("列2");
        rows.add("列3");
        ArrayList<Map<String, String>> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("列1", "data1" + String.valueOf(i));
            map.put("列2", "data2" + String.valueOf(i));
            map.put("列3", "data3" + String.valueOf(i));
            list.add(map);
        }
        Workbook workbook = null;
        try {
            workbook = ExcelUtil.excelUtil(rows, list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        try {
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + URLEncoder.encode("导入查询.xls", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
        } catch (IOException e) {
            throw new BusinessException("导出失败");
        }
    }


    @ApiOperation(value = "demo解析excel")
    @PostMapping("/excelImport")
    public ResponseResult<?> batchCreateInventory(@RequestPart("file") MultipartFile file) {

        TListener dateList = new TListener();
        try {
            EasyExcel.read(file.getInputStream(), ExampleModel.class, dateList).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        List<ExampleModel> returnList = dateList.getDate();

        return ResponseResult.success(returnList);
    }

}
